package com.sjz.han.industry.activites;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

//设置密码
public class AlterPwdActivity extends BaseActivity implements View.OnClickListener{
    private EditText alpwd_phone_edt,alpwd_code_edt,alpwd_newpwd_edt,alpwd_newpwdtwo_edt;
    private String phone="",code="",newpwd="",twopwd="";
    private TextView alpwd_getcode_tv;
    private Button alter_pwd_btn;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_alterpwd;
    }

    @Override
    protected void initDatas() {

    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("设置密码");

        alpwd_phone_edt=(EditText)this.findViewById(R.id.alpwd_phone_edt);
        alpwd_code_edt=(EditText)this.findViewById(R.id.alpwd_code_edt);
        alpwd_newpwd_edt=(EditText)this.findViewById(R.id.alpwd_newpwd_edt);
        alpwd_newpwdtwo_edt=(EditText)this.findViewById(R.id.alpwd_newpwdtwo_edt);

        alpwd_getcode_tv=(TextView)this.findViewById(R.id.alpwd_getcode_tv);
        alter_pwd_btn=(Button)this.findViewById(R.id.alter_pwd_btn);
    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
        alpwd_getcode_tv.setOnClickListener(this);
        alter_pwd_btn.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {

        phone=alpwd_phone_edt.getText().toString().trim();
                code=alpwd_code_edt.getText().toString().trim();
                newpwd=alpwd_newpwd_edt.getText().toString().trim();
                twopwd=alpwd_newpwdtwo_edt.getText().toString().trim();

        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
            case R.id.alpwd_getcode_tv:
                  if(TextUtils.isEmpty(phone)){
                      ToastUtils.popUpToast("手机号不可为空!");
                  }else{
                      getCheckCode(phone);
                  }
                break;
            case R.id.alter_pwd_btn:
                if(TextUtils.isEmpty(phone)){
                    ToastUtils.popUpToast("手机号不可为空!");
                }else if(TextUtils.isEmpty(code)){
                    ToastUtils.popUpToast("验证码不可为空!");
                }else if(TextUtils.isEmpty(newpwd)){
                    ToastUtils.popUpToast("新密码不可为空!");
                }else if(TextUtils.isEmpty(twopwd)){
                    ToastUtils.popUpToast("确认密码不可为空!");
                }else if(!newpwd.equals(twopwd)){
                    ToastUtils.popUpToast("两次输入密码不一致!");
                }else{
                    altger();
                }
                break;
        }
    }

    /**
     * ==========================获取验证码==============================
     * @param phone
     */
    private TimeCount timeCount=null;
    public void getCheckCode(String phone){
        timeCount = new TimeCount(60000,1000);
        timeCount.start();
        OkHttpUtils
                .post()
                .url(ReqestUrl.CHECKCODE_URL)
                .addParams("phone",phone)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                    }else{
                        ToastUtils.popUpToast("接口请求失败");
                    }
                }
            }
        });
    }
    //计时器
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInfuture,long countDownInterval){
            super(millisInfuture,countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            alpwd_getcode_tv.setText(millisUntilFinished/1000+"秒");
            alpwd_getcode_tv.setEnabled(false);
        }

        @Override
        public void onFinish() {
            alpwd_getcode_tv.setEnabled(true);
            alpwd_getcode_tv.setText("获取验证码");
        }
    }
    /**
     * ==========================获取验证码==============================
     */

    public void altger(){
        showLoadingDialog("正在修改...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.ALTER_PWD_URL)
                .addParams("phone",phone)
                .addParams("code",code)
                .addParams("pwd",newpwd)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                        finish();
                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
}
